package com.fenda.mybleapplication.study;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.SavedStateHandle;
import androidx.lifecycle.ViewModel;

public class ViewModelWithLiveData extends ViewModel {

    //AndroidViewModel 包含了上下文环境 可用来操作share 或 dao

    //安卓jetpack学习,  详情请看官网项目和开发文档
//    private MutableLiveData<Integer> LikedNumber;
    private SavedStateHandle handle;
    
    public ViewModelWithLiveData(SavedStateHandle handle){
        this.handle = handle;
    }

    public MutableLiveData<Integer> getLikedNumber() {

        if (!handle.contains("key")){
            handle.set("key",0);
        }
        return handle.getLiveData("key");
//        if (LikedNumber == null) {
//            LikedNumber = new MutableLiveData<>();
//            LikedNumber.setValue(0);
//        }
//        return LikedNumber;
    }

    //加减数据方法
    public void addLikedNumber(int n){
//        LikedNumber.setValue(LikedNumber.getValue()+n);
        getLikedNumber().setValue(getLikedNumber().getValue()+n);
    }

}
